<?php

function _ui_kentry_dbs($iso=NULL, $start='',$status=NULL) {
  if(trim($start)=="") {
    $start = $_GET['e'];
  }
  if($_GET['wg'] == '1'){//Sort relative priorities
    return _ui_kentry_change_grouping($iso, $start, $status);
  }else if( $_GET['wg'] == '2' ) {
    return _ui_kentry_edit_multiple($iso, $start, $status);
  }else if($iso) {
    return _ui_kentry_show_template($iso, $start, $status);
  }else {
    return _ui_kentry_show_all_templates(); 
  }
}

function _ui_kentry_change_grouping($iso, $start, $status) {
  $output = "<div class='title'>".
    t('modify display order of entries clustered with %group',
      array('%group'=>$start))."</div>";
      $output.= "<div>".
    t('If you have JavaScript enabled you can use the
      <span style="cursor: crosshair;">cross-hair handle at the
      beginning of each row</span> to move entries up or down to
      reorder them')."</div>";
      $output.= l(t('Return to %group', array('%group'=>$start)),
  "browse/$iso/$start", array('html'=>TRUE));
      $output.= drupal_get_form('kgrouping_form', $iso, $start);
      $output .= theme('pager', NULL, $page_increment);
      drupal_set_title($start." - " .drupal_get_title());
      return $output;
}

function _ui_kentry_edit_multiple($iso, $start, $status) {
      $output.= drupal_get_form('keditmultiple_form', $iso, $start);
      $output .= theme('pager', NULL, $page_increment);
      drupal_set_title($start." - " .drupal_get_title());
      return $output;
}

function _ui_kentry_show_template($iso, $start, $status) {
  $db = new KDatabase('kdictionary');
  $template = $db->RetrieveRow(array('wheres' => array("iso='$iso'")));
  drupal_set_title(t('@template - @start', 
                   array('@start' => $start?$start:'...', '@template' => $template->name)));
  $is_admin = user_access('create template');
  $output.= '<div id="fulldictionary">';
  $output.= theme('kentry_template', $template, true, $is_admin);
  $output.=  _ui_kentries($iso, $start, $status);
  $output.= '</div>';

  $breadcrumb[] = l(t('Home'), '');
  $breadcrumb[] = l(t('Browse'), 'browse');
  drupal_set_breadcrumb($breadcrumb);

  return $output;
}

function _ui_kentries($iso, $start, $status=1) {
  if($_GET['status'] == 'pending') {
    $status = 'pending';
  }
  if(trim($start)=="") {
    $start = $_GET['e'];
  }
  if($_GET['link'] == 'none') {
    $link = "none";
  }
  $matching_vids = _kentry_list($iso, $start, $status, $link);
  $entries = array();
  foreach($matching_vids as $vid) {
    $node = node_load(array('vid' => $vid->vid));
    if($node) {
      $entries[] = $node;
    }
  }
  $output.= theme('kentries', $header, $entries);
  return $output;
}

function _ui_kentry_show_all_templates() {
  $db = new KDatabase('kdictionary');
  $is_admin = user_access('create template');
  $wheres = $is_admin?array(1):array('status>0');
  $templates = $db->RetrieveRows(array('wheres' => $wheres, 'order' => array('name')));
  $output = t('Browse @site', array('@site' => variable_get('site_name', 'fie.nipa')));
  if($is_admin) {
    $output.= '<div class="warning">'.l(t('Administer'), 'admin/content/kw/kentry').'</div>';
  }
  foreach($templates as $template) {
    $output.= theme('kentry_template', $template, false, $is_admin);
  }
  $breadcrumb[] = l(t('Home'), '');
  drupal_set_breadcrumb($breadcrumb);
  return $output;
}

